<template>
  <el-dialog :destroy-on-close="true" :lock-scroll="false" title="设置安全库存" :visible.sync="visible" width="370px"
             :before-close="cancel">
    <el-form ref="form" :model="form" label-width="100px" :rules="rules">
      <el-form-item label="安全库存：" prop="inventory">
        <el-input-number v-model="form.inventory" type="number" :min="0"/>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    // 修改回显的数据
    data: Object
  },

  data() {
    return {
      visible: false,
      loading: false,
      id: null,
      // 表单数据
      form: {
        inventory: 0,
      },
      rules: {
        inventory: [
          {required: true, type: "number", min: 1, message: '请输入安全库存数量', trigger: 'blur'},
        ],
      },
    }
  },
  watch: {},
  methods: {
    active(id) {
      this.id = id
      this.visible = true;
    },
    cancel() {
      this.visible = false;
      this.loading = false
      this.$refs.form.resetFields()
    },
    /*保存*/
    save(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http.post(`/inventories/${this.id}/safe_quantity`, this.form).then((res) => {
            if (res.data.code === 0) {
              this.cancel()
              this.$emit('reload')
              this.$message.success(res.data.msg)
            } else this.$message.error(res.data.msg)
            this.cancel()
          })
          /*发起请求*/
        } else {
          return false;
        }
      });
    },

  },
}
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
